// 实现这个项目的构建任务
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {

  grunt.initConfig({
    clean: ['dist'],
    sass: {
      dist: {
        files: [{
          expand: true,
          cwd: 'src/assets/styles',
          src: ['*.scss'],
          dest: '.temp/src/assets/styles',
          ext: '.css'
        }]
      }
    },
    cssmin: {
      target: {
        files: [{
          expand: true,
          cwd: '.temp/src/assets/styles',
          src: ['*.css', '!*.min.css'],
          dest: 'dist/src/assets/styles',
          ext: '.min.css'
        }]
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      main: {
        files: {
          '.temp/src/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    uglify: {
      target: {
        files: [{
          expand: true,
          cwd: '.temp/src/assets/scripts',
          src: '*.js',
          dest: 'dist/src/assets/scripts',
          ext: '.min.js'
        }]
      }
    },
    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true
        },
        files: [{
          expand: true,
          cwd: 'src',
          src: ['**/*.html', '*.html'],
          dest: 'dist/src'
        }]
      }
    },
    imagemin: {
      static: {
        options: {
          optimizationLevel: 3,
          svgoPlugins: [{
            removeViewBox: false
          }]
        },
         files: [{
           expand: true,
           cwd: 'src',
           src: ['assets/images/*.{png,jpg,svg}'],
           dest: 'dist/src'
         }]
      }
    },
    copy: {
      fonts: {
        src: 'src/assets/fonts/*.{eot,svg,ttf,woff}',
        dest: 'dist/'
      },
      public: {
        src: 'public/favicon.ico',
        dest: 'dist/'
      } 
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
      html: {
        files: ['src/**/*.html', '*.html'],
        tasks: ['htmlmin']
      },
      image: {
        files: ['src/assets/images/*.png', 'src/assets/images/*.jpg', 'src/assets/images/*.gif', 'src/assets/images/*.svg'],
        tasks: ['imagemin']
      }
    },
  })
  
  
  loadGruntTasks(grunt)
  grunt.registerTask('default', ['clean', 'sass', 'babel', 'htmlmin', 'imagemin', 'copy', 'cssmin', 'uglify'])
}
